import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'

import '@/assets/css/details.css'
import '@/assets/css/font-awesome.min.css'
import '@/assets/css/index.css'
import '@/assets/css/list.css'
import '@/assets/css/login.css'
import '@/assets/css/public-head.css'
import '@/assets/css/reset.css'
import '@/assets/css/swiper.css'
import '@/assets/css/user_center.css'
import '@/assets/css/user_pay.css'


Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')


/* 请求拦截器：发送请求前拦截 */
axios.interceptors.request.use(function(config){
    //判断需要token的访问服务器地址，加入token
    console.log( "axios.interceptors.request url="+config.url);
    if('/api/v1/user/realname'== config.url || '/api/v1/user/info' == config.url||'/api/v1/user/logout'==config.url) {
        //在Header中添加token Authorization: Bearer <token>
        //从sessonStorage获取token
        if(window.sessionStorage.getItem("accessToken")){
			/* 获取 会话存储空间的 token */
            config.headers['Authorization']="Bearer " + window.sessionStorage.getItem("accessToken");
            config.headers['uid']= JSON.parse(window.sessionStorage.getItem("userInfo")).uid;
        }
    }
    return config;
},function(err){
	
})

/* 响应拦截器 ： 当强求有响应时 执行*/
axios.interceptors.response.use(function(resp){
	/* 如果状态码是4003说明 它是被 拦截器拦截了   没有token   需要去登录 */
    if(resp.data.code==4003){
		/* 重新登录 */
		setTimeout(function(){
			window.location.href="/page/login"
		},500)
	}
    return resp;
},function(err){
	
})